{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 语法\n",
    "file = open('文件路径','文件打开模式')\n",
    "\n",
    "# 打开模式\n",
    "# r  只读\n",
    "# r+ 读写\n",
    "# w  写入、覆盖\n",
    "# w+ 读写、覆盖\n",
    "# a  追加写入、不覆盖\n",
    "# a+ 追加读写、不覆盖\n",
    "# b  以二进制方式打开，可以结合r/w/a 使用\n",
    "# U  支持所有换行符号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'tenant': {'uuid': '8a1d23e578f62f200178f73dc1191519', 'code': 'ywh133', 'name': '原租户'}, 'contract': {'uuid': 'b83a7fbe-9f95-4b3d-b28b-08e2899e7389', 'code': '2020fzc0012104210001', 'name': '原租户'}, 'payTotal': 6000, 'unpayedTotal': 6000, 'payedTotal': 0, 'positions': [{'uuid': '8a1d23e578f380110178f3aedf510176', 'code': 'dsj001', 'name': '大世界单元001'}]}]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "# 读取json文件\n",
    "with open('response.json',encoding='utf8') as file:\n",
    "    file_1 = json.loads(file.read())\n",
    "    print(file_1['data']['contractDetails'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022/2/10 18:40\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 读取csv文件\n",
    "with open('BankAccount.csv') as file:\n",
    "    # 先读取第一行后，查看指针位置在 140\n",
    "    # file.readline()\n",
    "    # print(file.tell()) ==> 140\n",
    "    # 将指针移到141，即从第二行开始逐行读取\n",
    "    file.seek(141)\n",
    "    file_1 = file.readlines()\n",
    "    # 读取的结果是一个列表，每行数据是一个字符串，将每行数据以逗号进行分割，放入新的列表\n",
    "    list_1 = [i.split(',') for i in file_1]\n",
    "    print(list_1[0][1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# EXCEL 文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install python-docx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Word 文件\n",
    "from docx import Document\n",
    "\n",
    "doc = Document()\n",
    "doc.add_heading('docx 标题', 0)\n",
    "\n",
    "# 添加表格\n",
    "records = (\n",
    "    (3, '101', 'Spam'),\n",
    "    (7, '422', 'Eggs'),\n",
    "    (4, '631', 'Spam'),\n",
    "    (6, '128', 'Spam'),\n",
    ")\n",
    "table = doc.add_table(rows=1, cols=3)\n",
    "# 表格首行\n",
    "hdr_cells = table.rows[0].cells\n",
    "hdr_cells[0].text = 'Qty'\n",
    "hdr_cells[1].text = 'Id'\n",
    "hdr_cells[2].text = 'Desc'\n",
    "\n",
    "# 表格数据\n",
    "for qty, id, desc in records:\n",
    "    row_cells = table.add_row().cells\n",
    "    row_cells[0].text = str(qty)\n",
    "    row_cells[1].text = id\n",
    "    row_cells[2].text = desc\n",
    "\n",
    "doc.add_page_break()\n",
    "\n",
    "doc.save('doccc.docx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aqiyi.jpg\n"
     ]
    }
   ],
   "source": [
    "# 过滤出指定后缀格式的文件\n",
    "from pathlib import PurePath, Path\n",
    "\n",
    "# 当前路径， .. \n",
    "p = Path('.')\n",
    "\n",
    "for x in p.iterdir():\n",
    "    if PurePath(x).match('*.jpg'):\n",
    "        print(PurePath(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读写 EXCEL \n",
    "from openpyxl import Workbook, load_workbook\n",
    "\n",
    "# 批量读取 Excel 文件\n",
    "files = [ x for x in p.iterdir() if PurePath(x).match('*.xlsx') ]\n",
    "for file in files:\n",
    "    wb = load_workbook(file)\n",
    "    ws = wb.active  # 激活 sheet\n",
    "    for row in ws.iter_rows():\n",
    "        # 按 单元格读取\n",
    "        for cell in row:\n",
    "            print(cell.value)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['2月', '3月']\n",
      "('账户', '招商信用卡', '浦发信用卡', '花呗', '余额宝', '股票', '存款')\n",
      "('总额', '=SUM(B3:B5)', None, None, 19159.63, 33000, '=SUM(B2:F2)')\n",
      "('当期', -13235.82, 0, None, 111, None, 111)\n",
      "('下期', -5367.18, None, None, 8000, 500, 1000)\n",
      "('下期', -5367.17, None, 2000, 111, 222, 222)\n",
      "('账户', '招商信用卡', '浦发信用卡', '花呗', '余额宝', '股票', '存款')\n",
      "('总额', 0, None, None, 0, 0, 10000)\n",
      "('当期', 1, 0, None, 1, 1, 10001)\n",
      "('下期', 2, None, None, 2, 2, 10002)\n",
      "('下期', 3, None, 2000, 3, 3, 10003)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\13675\\AppData\\Local\\Temp\\ipykernel_8300\\1026220443.py:13: DeprecationWarning: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).\n",
      "  s1 = wb.get_sheet_by_name('2月')\n"
     ]
    }
   ],
   "source": [
    "# 读取单个Excel文件\n",
    "from openpyxl import Workbook, load_workbook\n",
    "\n",
    "wb = load_workbook('C:\\\\Users\\\\13675\\\\Desktop\\\\账户.xlsx')\n",
    "# 获取所有 sheet 名称\n",
    "print(wb.sheetnames)\n",
    "\n",
    "# 激活 sheet\n",
    "wb.active\n",
    "\n",
    "# 获取指定名称的sheet\n",
    "s1 = wb['2月']\n",
    "\n",
    "# 获取指定索引的 sheet\n",
    "s2 = wb.worksheets[1]\n",
    "\n",
    "\n",
    "\n",
    "# 打印两个sheet的内容，按行读取\n",
    "for row in s1.values:\n",
    "    print(row)\n",
    "\n",
    "for row in s2.values:\n",
    "    print(row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在上一步读取的文件写入新sheet\n",
    "wb.create_sheet('4月')\n",
    "s_new = wb['4月']\n",
    "s_new.append(\n",
    "    ('账户', '招商信用卡', '浦发信用卡', '花呗', '余额宝', '股票', '存款')\n",
    ")\n",
    "wb.save('C:\\\\Users\\\\13675\\\\Desktop\\\\账户.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 新建工作簿 写入文件\n",
    "wb = Workbook()\n",
    "\n",
    "ws = wb.active # 获取激活的工作表，默认第一个\n",
    "# 设置sheet 名称\n",
    "ws.title = '一年级'\n",
    "ws.append(('姓名', '学号', '班级'))\n",
    "ws.append(('小明', '20220916', '一班'))\n",
    "\n",
    "# 复制 sheet0\n",
    "ws2 = wb.copy_worksheet(wb.worksheets[0])\n",
    "ws2.title = '二年级'\n",
    "\n",
    "# 新建 sheet\n",
    "ws3 = wb.create_sheet(title='汇总')\n",
    "ws3.append(('姓名', '学号', '班级', '总排名'))\n",
    "\n",
    "# 保存\n",
    "wb.save(r'班级成绩单.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "class StaticClass:\n",
    "    var = 'Chrome'\n",
    "\n",
    "    def func():\n",
    "        print('静态函数')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'FireFox'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "StaticClass.var\n",
    "# StaticClass.func()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = StaticClass()\n",
    "s.var"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "s.var = 'Sougou'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'FireFox'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "StaticClass.var"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "func() takes 0 positional arguments but 1 was given",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_12208\\3304246249.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: func() takes 0 positional arguments but 1 was given"
     ]
    }
   ],
   "source": [
    "s.func()"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "ff79ebfcf6806606a412477be2872c3ec52671763ecb054ca3010c66083164a3"
  },
  "kernelspec": {
   "display_name": "Python 3.8.3 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
